package config

import (
	"fmt"

	"github.com/spf13/viper"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
)

const (
	TimeFormat string = "2006-01-02 15:04:05"
	// Dsn        string = "host=172.16.99.134 user=lic password=lichang123 dbname=psqldb port=5432 sslmode=disable"
)

var DeploymentJson = `{
    "kind": "Deployment",
    "apiVersion": "apps/v1",
    "metadata": {
        "name": "nginx",
        "namespace": "yunkuai",
        "creationTimestamp": null,
        "labels": {
            "app": "nginx"
        }
    },
    "spec": {
        "replicas": 1,
        "selector": {
            "matchLabels": {
                "app": "nginx"
            }
        },
        "template": {
            "metadata": {
                "creationTimestamp": null,
                "labels": {
                    "app": "nginx"
                }
            },
            "spec": {
                "containers": [
                    {
                        "name": "nginx",
                        "image": "registry.openanolis.cn/openanolis/nginx:1.14.1-8.6",
                        "resources": {}
                    }
                ]
            }
        },
        "strategy": {}
    },
    "status": {}
}
`

var (
	JwtSignKey    string
	JwtExpireTime int64
	TokenString   string
	ClientSet     *kubernetes.Clientset
)

type ReturnData struct {
	Status  int                    `json:"status"`
	Message string                 `json:"message"`
	Data    map[string]interface{} `json:"data"`
}

func ClientSetInit() {
	//解析配置文件
	restconfig, err := clientcmd.BuildConfigFromFlags("", "./config/meta.config")
	if err != nil {
		msg := "Failed to parse kubeconfig file" + err.Error()
		fmt.Println(msg)
		panic(err.Error())
	}

	//创建客户端工具
	clientset, err := kubernetes.NewForConfig(restconfig)
	if err != nil {
		msg := "Failed to create clientset" + err.Error()
		fmt.Println(msg)
		panic(err.Error())
	}

	//获取集群版本
	serverversion, err := clientset.Discovery().ServerVersion()
	if err != nil {
		panic(err.Error())
	}

	clientset.Discovery().RESTClient()
	clusterVersion := serverversion.String()
	fmt.Println("version", clusterVersion)
	ClientSet = clientset
}

func init() {
	ClientSetInit()
	fmt.Println("开始加载程序配置")
	viper.SetDefault("JWTSIGNKEY", "xxxxxx")
	viper.SetDefault("JwtExpireTime", 120)
	viper.AutomaticEnv()
	JwtSignKey = viper.GetString("JWTSIGNKEY")
	fmt.Println("打印JwtSignKey", JwtSignKey)
	JwtExpireTime = viper.GetInt64("JwtExpireTime")

}
